<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>等值面 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars3d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            center: { lat: 23.359088, lng: 116.19963, alt: 1262727, heading: 2, pitch: -60 },
          },
          layers: [],
        });

        //创建三维地球场景
        map = new mars3d.Map("mars3dContainer", mapOptions);

        map.imageryLayers._layers.forEach(function (layer, index, arr) {
          layer.brightness = 0.4;
        });

        //加载气象
        queryWindpointApiData()
          .then(function (res) {
            showWindLine(res.data);
          })
          .catch(function () {
            haoutil.msg("实时查询气象信息失败，请稍候再试");
          });
      }

      var colors = ["#006837", "#1a9850", "#66bd63", "#a6d96a", "#d9ef8b", "#ffffbf", "#fee08b", "#fdae61", "#f46d43", "#d73027", "#a50026"];
      var breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 99]; //等值面的级数

      // 访问后端接口，取数据
      function queryWindpointApiData() {
        return new Promise(function (resolve, reject) {
          $.ajax({
            url: "//data.mars3d.cn/file/apidemo/windpoint.json",
            type: "get",
            dataType: "json",
            success: function (result) {
              resolve(result);
            },
            error: function (data) {
              reject(data);
            },
          });
        });
      }

      //等值线面
      function showWindLine(arr) {
        // var min = arr[0].speed
        // var max = arr[0].speed

        var pointGrid = [];
        for (let i = 0, len = arr.length; i < len; i++) {
          const item = arr[i];

          // if (min > item.speed) min = item.speed
          // if (max < item.speed) max = item.speed

          pointGrid.push({
            type: "Feature",
            properties: item,
            geometry: {
              type: "Point",
              coordinates: [item.x, item.y],
            },
          });
        }

        // breaks = []
        // var step = (max - min) / 10
        // for (var i = min; i <= max; i += step) {
        //   breaks.push(Number(i.toFixed(1)))
        // }

        var points = {
          type: "FeatureCollection",
          features: pointGrid,
        };

        //插值
        // points = turf.interpolate(points, 10, {
        //   gridType: 'point', // 'square' | 'point' | 'hex' | 'triangle'
        //   property: 'speed',
        //   units: 'kilometers', // degrees, radians, miles, or kilometers
        //   weight: 1
        // })
        // 适当降低插值结果的精度便于显示
        // points.features.map((i) => (i.properties.speed = Number(i.properties.speed.toFixed(2))))

        //等值面
        var geojsonPoly = turf.isobands(points, breaks, {
          zProperty: "speed",
        });

        var geoJsonLayer = new mars3d.layer.GeoJsonLayer({
          name: "等值面",
          data: geojsonPoly,
          popup: "{speed}",
          symbol: {
            styleOptions: {
              fill: true, //是否填充
              color: "#ffff00", //颜色
              opacity: 0.7, //透明度
            },
            callback: function (attr, styleOpt) {
              //得到点的权重，计算落在那个色度带
              let val = Number(attr.speed.split("-")[0] || 0);
              let color = getColor(val);
              return {
                color: color,
              };
            },
          },
        });
        map.addLayer(geoJsonLayer);

        //等值线
        var geojsonLine = turf.isolines(points, breaks, {
          zProperty: "speed",
        });

        //进行平滑处理
        // var features = geojsonLine.features;
        // for (var i = 0; i < features.length; i++) {
        //     var _coords = features[i].geometry.coordinates;
        //     var _lCoords = [];
        //     for (var j = 0; j < _coords.length; j++) {
        //         var _coord = _coords[j];
        //         var line = turf.lineString(_coord);
        //         var curved = turf.bezierSpline(line);
        //         _lCoords.push(curved.geometry.coordinates);
        //     }
        //     features[i].geometry.coordinates = _lCoords;
        // }

        var layerDZX = new mars3d.layer.GeoJsonLayer({
          name: "等值线",
          data: geojsonLine,
          popup: "{speed}",
          symbol: {
            styleOptions: {
              width: 2, //边框宽度
              color: "#000000", //边框颜色
              opacity: 0.5, //边框透明度
              clampToGround: false, //是否贴地
            },
          },
        });
        map.addLayer(layerDZX);
      }

      function getColor(value) {
        for (var i = 0; i < breaks.length; i++) {
          if (breaks[i] === value) {
            return colors[i];
          }
        }
        return colors[0];
      }
    </script>
  </body>
</html>
